package com.apex.wx.controller;

import com.apex.common.controller.BaseController;
import com.apex.security.api.entity.Pager;
import com.apex.security.api.entity.TDispatch;
import com.apex.wx.service.ScheduleCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mobile.device.site.SitePreference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Create by IntelliJ IDEA
 * 用户：王建
 * 日期：2018-4-8
 */
@Controller
@RequestMapping("/uc/schedule")
public class UserCenterController extends BaseController {


    @Autowired
    private ScheduleCenterService scheduleCenterService;

    @RequestMapping("/index")
    public String scheduleIndex(ModelMap modelMap, HttpServletRequest request) {
        scheduleCenterService.wxIndex(modelMap, "admin");
        return "mobile/index/schedule/index";
    }

    /**
     * 个人待办
     *
     * @param modelMap
     * @param request
     * @return
     */
    @RequestMapping("/list/db")
    public String getAllUserDb(ModelMap modelMap, HttpServletRequest request, Pager pager, String title) {
        modelMap.put("title", "我的待办");
        scheduleCenterService.userAllDb(modelMap, "admin", title, pager);
        return "mobile/index/schedule/entity";
    }

    /**
     * 个人已办
     *
     * @param modelMap
     * @param request
     * @param pager
     * @param title
     * @return
     */
    @RequestMapping("/list/yb")
    public String getAllUserYb(SitePreference sitePreference, ModelMap modelMap, HttpServletRequest request, Pager pager, String title) {
        modelMap.put("title", "事项追踪");
        scheduleCenterService.userAllYb(modelMap, "admin", title, pager);
        return "mobile/index/schedule/entity";
    }

    /**
     * 查看学院发文（根据用户身份）
     *
     * @param modelMap
     * @param request
     * @param pager
     * @param title
     * @return
     */
    @RequestMapping("/list/dispatch")
    public String getUserAllDispatch(SitePreference sitePreference, ModelMap modelMap, HttpServletRequest request, Pager pager, String title) {
        scheduleCenterService.userAllDispatch(modelMap, getUserId(request), title, pager);
        if (sitePreference != SitePreference.NORMAL) {
            return "mobile/index/schedule/list";
        }
        return "index/schedule/dispatch";
    }

    /**
     * 查看单个发文
     *
     * @param modelMap
     * @param request
     * @param id
     * @return
     */
    @RequestMapping("/entity/{id}/dispatch")
    public String getUserDispatch(SitePreference sitePreference, ModelMap modelMap, HttpServletRequest request, @PathVariable("id") Long id) {
        scheduleCenterService.userDispatch(modelMap, getUserId(request), id);
        modelMap.put("title", "学校发文");
        TDispatch dispatch = (TDispatch) modelMap.get("entity");
        if (null != dispatch && !StringUtils.isEmpty(dispatch.getFjdz())) {
            return "redirect:" + dispatch.getFjdz();
        }
        return "mobile/index/schedule/error";

    }

    @RequestMapping("/application/ajax/summary")
    @ResponseBody
    public String getAllUserSummary(HttpServletRequest request) {
        return scheduleCenterService.userSummary(getUserId(request));
    }

    /**
     * 个人中心异步获取待办信息
     *
     * @param request
     * @return
     */
    @RequestMapping("/ajax/user/db")
    @ResponseBody
    public String getAjaxUserDb(HttpServletRequest request, String title, int content) {
        return scheduleCenterService.userAllDb("admin", title, content);
    }

    /**
     * 个人中心异步获取个人已办信息
     *
     * @param request
     * @return
     */
    @RequestMapping("/ajax/user/yb")
    @ResponseBody
    public String getAjaxUserYb(HttpServletRequest request, String title, int content) {
        return scheduleCenterService.userAllYb("admin", title, content);
    }

    /**
     * 个人消息提醒
     *
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping("/user/news")
    public String getAllNews(HttpServletRequest request, ModelMap modelMap) {
        scheduleCenterService.newsEntity(request, modelMap, "admin");
        return "mobile/index/schedule/news";
    }

    /**
     * 异步获取个人通知公告信息
     *
     * @param request
     * @return
     */
    @RequestMapping("/ajax/user/dispatch")
    @ResponseBody
    public String getAjaxUserDispatch(HttpServletRequest request, String title, int content) {
        return scheduleCenterService.userDispatch(getUserId(request), title, content);
    }

    @RequestMapping("/ajax/user/lecture")
    @ResponseBody
    public String getAllLecture(HttpServletRequest request, String title, int content) {
        return scheduleCenterService.userAllLecture(getUserId(request), null, title, content);
    }

    /**
     * 查看全部学院讲座（根据用户身份）
     *
     * @param modelMap
     * @param request
     * @param title
     * @param pager
     * @return
     */
    @RequestMapping("/list/lecture")
    public String getUserAllLecture(ModelMap modelMap, HttpServletRequest request, String title, Pager pager) {
        scheduleCenterService.userAllLecture(modelMap, getUserId(request), title, pager);

        return "mobile/index/schedule/list";

    }

    /**
     * 查看单个讲座
     *
     * @param modelMap
     * @param request
     * @param id
     * @return
     */
    @RequestMapping("/entity/{id}/lecture")
    public String getUserLecture(ModelMap modelMap, HttpServletRequest request, @PathVariable("id") Long id) {
        scheduleCenterService.userLecture(modelMap, getUserId(request), id);
        modelMap.put("title", "学校讲座");
        return "mobile/index/schedule/lecture";

    }
}
